package com.tuanzi.loan.business.repository.onekey.impl;

import com.tuanzi.loan.business.repository.onekey.custom.CtrlOneKeyOnlineRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.vo.request.family.CtrlOneKeyOnlineRequest;
import com.tuanzi.loan.vo.response.family.CtrlOneKeyOnlineResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanCtrlOneKeyOnline.LOAN_CTRL_ONE_KEY_ONLINE;

/**
 * @author caiwanlin
 * @create 2018-07-12 11:46
 */
public class CtrlOneKeyOnlineRepositoryImpl extends JOOQBaseRepositoryPlus implements CtrlOneKeyOnlineRepositoryCustom {
    @Override
    protected TableImpl getTable() {
        return LOAN_CTRL_ONE_KEY_ONLINE;
    }

    @Override
    public Page<CtrlOneKeyOnlineResponse> list(CtrlOneKeyOnlineRequest ctrlOneKeyOnlineRequest, Pageable pageable) {
        String partyName = ctrlOneKeyOnlineRequest.getPartyName();
        Integer apiTemplateId = ctrlOneKeyOnlineRequest.getApiTemplateId();
        Integer status = ctrlOneKeyOnlineRequest.getStatus();
        Integer numId = ctrlOneKeyOnlineRequest.getNumId();
        fields(LOAN_CTRL_ONE_KEY_ONLINE.ID,LOAN_CTRL_ONE_KEY_ONLINE.PARTY_NAME,
                LOAN_CTRL_ONE_KEY_ONLINE.NUM_TYPE,LOAN_CTRL_ONE_KEY_ONLINE.NUM_ID,
                LOAN_CTRL_ONE_KEY_ONLINE.API_TEMPLATE_ID,LOAN_CTRL_ONE_KEY_ONLINE.STATUS,
                LOAN_CTRL_ONE_KEY_ONLINE.OFFLINE_COUNT,LOAN_CTRL_ONE_KEY_ONLINE.CONTROL_TYPE,
                LOAN_CTRL_ONE_KEY_ONLINE.ONLINE_TIME,LOAN_CTRL_ONE_KEY_ONLINE.OFFLINE_TIME,
                LOAN_CTRL_ONE_KEY_ONLINE.UPDATOR,LOAN_CTRL_ONE_KEY_ONLINE.UPDATE_TIME);
        contains(LOAN_CTRL_ONE_KEY_ONLINE.PARTY_NAME,partyName);
        eq(LOAN_CTRL_ONE_KEY_ONLINE.API_TEMPLATE_ID,apiTemplateId);
        eq(LOAN_CTRL_ONE_KEY_ONLINE.STATUS,status);
        eq(LOAN_CTRL_ONE_KEY_ONLINE.NUM_ID,numId);
        orderBy(LOAN_CTRL_ONE_KEY_ONLINE.STATUS, Sort.Direction.DESC);
        Page<CtrlOneKeyOnlineResponse> search = search(pageable, record -> {
            CtrlOneKeyOnlineResponse response = new CtrlOneKeyOnlineResponse();
            response.setId(record.get(LOAN_CTRL_ONE_KEY_ONLINE.ID));
            response.setPartyName(record.get(LOAN_CTRL_ONE_KEY_ONLINE.PARTY_NAME));
            response.setNumType(record.get(LOAN_CTRL_ONE_KEY_ONLINE.NUM_TYPE));
            response.setNumId(record.get(LOAN_CTRL_ONE_KEY_ONLINE.NUM_ID));
            response.setApiTemplateId(record.get(LOAN_CTRL_ONE_KEY_ONLINE.API_TEMPLATE_ID));
            response.setStatus(record.get(LOAN_CTRL_ONE_KEY_ONLINE.STATUS));
            response.setOfflineCount(record.get(LOAN_CTRL_ONE_KEY_ONLINE.OFFLINE_COUNT));
            response.setControlType(record.get(LOAN_CTRL_ONE_KEY_ONLINE.CONTROL_TYPE));
            response.setOnlineTime(record.get(LOAN_CTRL_ONE_KEY_ONLINE.ONLINE_TIME));
            response.setOfflineTime(record.get(LOAN_CTRL_ONE_KEY_ONLINE.OFFLINE_TIME));
            response.setUpdator(record.get(LOAN_CTRL_ONE_KEY_ONLINE.UPDATOR));
            response.setUpdateTime(record.get(LOAN_CTRL_ONE_KEY_ONLINE.UPDATE_TIME));
            return response;
        });
        return search;
    }
}
